package org.dftproject.genesis.ui.presentation.stackcontrol;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

public class StackControl extends Composite {

	private final Canvas stackHeaderCanvas;
	private StackHeaderFigure stackHeaderFigure;
	
	private final Canvas stackFooterCanvas;
	private StackFooterFigure stackFooterFigure;
	
	private final ArrayList<StackItem> items = new ArrayList<StackItem>();
	
	private final List<IStackControlListener> listeners = new LinkedList<IStackControlListener>(); 

	private StackItem selectedStackItem;
	
	public StackControl(Composite parent, int style) {
		super(parent, style);
		
		stackHeaderCanvas = new Canvas(this, SWT.DOUBLE_BUFFERED | SWT.NO_BACKGROUND);
		
		LightweightSystem lws = new LightweightSystem(stackHeaderCanvas);
		lws.setContents(stackHeaderFigure = new StackHeaderFigure());
		
		stackFooterCanvas = new Canvas(this, SWT.DOUBLE_BUFFERED | SWT.NO_BACKGROUND);
		
		lws = new LightweightSystem(stackFooterCanvas);
		lws.setContents(stackFooterFigure = new StackFooterFigure());
		
		addControlListener(new ControlListener() {

			public void controlMoved(ControlEvent e) {
			}

			public void controlResized(ControlEvent e) {
				Point size = getSize();
				Dimension headerSize = stackHeaderFigure.getPreferredSize(size.x, -1);
				Dimension footerSize = stackFooterFigure.getPreferredSize(size.x, -1);
				stackHeaderCanvas.setBounds(0, 0, size.x, headerSize.height);
				stackFooterCanvas.setBounds(0, size.y - footerSize.height, size.x, footerSize.height);
			}
			
		});
		addPaintListener(new PaintListener() {

			public void paintControl(PaintEvent e) {
				Point size = getSize();
				Rectangle viewsBounds = stackHeaderCanvas.getBounds();
				
				e.gc.setForeground(new Color(null, 0x55, 0x55, 0x55));
				e.gc.drawLine(0, viewsBounds.height, 0, size.y - 2);
				e.gc.drawLine(size.x - 1, viewsBounds.height, size.x - 1, size.y - 2);
			}
			
		});
	}

	protected void createStackItem(StackItem item) {
		items.add(item);
	}

	public StackItem[] getStackItems() {
		return items.toArray(new StackItem[0]);
	}

	public void setSelection(StackItem item) {
		//
	}

	@Override
	public Rectangle getClientArea() {
		Point size = getSize();
		Rectangle headerBounds = stackHeaderCanvas.getBounds();
		Rectangle footerBounds = stackFooterCanvas.getBounds();
		Rectangle clientArea = new Rectangle(1, headerBounds.height, size.x - 2, size.y - headerBounds.height - footerBounds.height);
		return clientArea;
	}

	public void closeStackItem(StackItem item) {
		if (item == null)
			return;
		
		items.remove(item);
		item.dispose();
	}
	
	public void addStackControlListener(IStackControlListener listener) {
		listeners.add(listener);
	}
	
	public void removeStackControlListener(IStackControlListener listener) {
		listeners.remove(listener);
	}

	public StackHeaderFigure getStackItemHeaderFigure(StackItem item) {
		return stackHeaderFigure;
	}
	
}
